package com.qs.controller;


import com.qs.annotation.OperationLog;
import com.qs.pojo.dto.CartDto;
import com.qs.pojo.dto.GoodsDto;
import com.qs.pojo.dto.GoodsOrderDto;
import com.qs.pojo.entity.Cart;
import com.qs.response.Response;
import com.qs.service.ICartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-12-03
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {

    private final ICartService cartService;

    /**
     * 获取订单页购物车商品数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/order/{ids}")
    public Response getOrderGoods(@PathVariable("ids") List<Integer> ids) {
        log.info("获取订单页购物车商品数据请求进来--------------------");
        Map<String,Object> goodsDtoList = cartService.getOrderGoods(ids);
        return Response.success(goodsDtoList);
    }

    /**
     * 从购物车转去付款接口获取订单号
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/get/orderId/{ids}/{userId}")
    public Response getOrderId(@PathVariable("ids") List<Integer> ids,
                               @PathVariable("userId") Integer userId) {
        log.info("从购物车转去付款接口获取订单号请求进来--------------------");
        String orderId = cartService.getOrderId(ids,userId);
        return Response.success(orderId);
    }

    /**
     * 添加购物车
     * @param cart
     * @return
     */
    @PostMapping("/add")
    public Response addCart(@RequestBody Cart cart) {
        log.info("添加购物车请求进来-----------------");
        cartService.addCart(cart);
        return Response.success();
    }

    /**
     * 获取当前用户的购物车数据
     * @param userId
     * @return
     */
    @GetMapping("/list/{userId}")
    public Response list(@PathVariable("userId") Integer userId) {
        log.info("获取当前用户的购物车数据请求进来-----------------");
        Map<String,Object> cartDtoMap = cartService.getList(userId);
        return Response.success(cartDtoMap);
    }

    /**
     * 修改购物车中的商品数量
     * @param id
     * @return
     */
    @PutMapping("/update/{id}/{number}")
    public Response updateCartById(@PathVariable("id") Integer id,@PathVariable("number") Integer number) {
        log.info("修改购物车中的商品数量请求进来-----------------");
        cartService.updateCartById(id,number);
        return Response.success();
    }

    /**
     * 批量删除购物车
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public Response remove(@PathVariable("id") Integer id) {
        log.info("批量删除购物车数据请求进来-----------------");
        cartService.removeById(id);
        return Response.success();
    }
}
